import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
import util from '../../../utils/util';

const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.uploadsUrl,
    note: '',
    address: '',
    originUpImgs: [],
    originDownImgs: [],
    upImgs: [],
    isIphoneX: app.globalData.isIphoneX,
    coverImgs: [],
    cententDetail: {
      certType: 1,
    },
    certTypeIndex: 0,
    certTypeList: [{
        id: 1,
        name: '身份证'
      },
      {
        id: 2,
        name: '护照'
      },
      {
        id: 3,
        name: '港澳通行证'
      },
      {
        id: 4,
        name: '台胞证'
      }
    ],
    idcardFrontPath: '',
    idcardBackPath: '',
    paramsId: '',
    isEdit: false,
    isDelImg: false,
  },


  //选择身份证正面图片
  chooseUpGoodsImg: function (e) {
    this.setData({
      upImgs: this.uploadUpImgs.data.imgs,
      isDelImg: true
    })

  },

  deleteUpGoodsImg: function (e) {
    this.setData({
      upImgs: this.uploadUpImgs.data.imgs
    })
  },

  deloriginUpGoodsImg: function () {
    this.setData({
      originUpImgs: this.uploadImgs.data.originImgs
    })
  },

  //选择身份证反面图片
  chooseDownGoodsImg: function (e) {
    this.setData({
      downImgs: this.uploadDownImgs.data.imgs,
      isDelImg: true
    })
  },

  deleteDownGoodsImg: function (e) {
    this.setData({
      downImgs: this.uploadDownImgs.data.imgs
    })
  },

  deloriginDownGoodsImg: function () {
    this.setData({
      originDownImgs: this.uploadDownImgs.data.originImgs
    })
  },

  //选择类型
  typePickerChange: function (e) {
    var certTypeIndex = e.detail.value;
    var terminalList = this.data.certTypeList
    var typeId = '';

    terminalList.map((item, index) => {
      if (index == certTypeIndex) {
        typeId = item.id
      }
    })

    this.setData({
      certTypeIndex,
      ["cententDetail.certType"]: typeId
    })
  },

  // 页面滚动
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    });
  },

  textareaBlur: function (e) {
    var value = e.detail.value
    this.setData({
      note: value
    })
  },

  //检查用户是否授权
  checkLocationAuth() {
    const self = this
    //获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userLocation']) {
          self.setData({
            canUseLocation: true,
          })
        } else {
          console.log("用户授权")
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              self.setData({
                canUseLocation: true,
              })
            },
            fail() {
              wx.showToast({
                icon: "error",
                title: '请允许获取字母歌小程序您的地理位置',
              })
            }
          })
        }
      }
    })
  },

  onShow() {
    this.checkLocationAuth();
  },

  //选择详情地址
  chooseAddress() {
    const self = this
    if (!this.data.canUseLocation) {
      this.checkLocationAuth()
    }
    wx.chooseLocation({
      success: function (re) {
        console.log(re)
        self.setData({
          detailAddress: re.address,
          newAddressAlias: util.address2City(re.address),
          latitude: re.latitude,
          longitude: re.longitude,
        })
      },
      fail: function (re) {
        var msg = re.errMsg
        var reg = /auth deny/
        if (reg.test(msg)) {
          console.log("权限不足需要提示用户授权")
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if (options.id) {
      this.data.paramsId = options.id
    }

    this.uploadUpImgs = this.selectComponent("#uploadUpImgs");
    console.log(this.uploadUpImgs);

    this.uploadDownImgs = this.selectComponent("#uploadDownImgs");

    if (options.isEdit) {
      this.data.cententDetail = wx.getStorageSync('editList')
      let backImg = this.data.imgUrl + this.data.cententDetail.idcardBackPath
      let frontImg = this.data.imgUrl + this.data.cententDetail.idcardFrontPath
      this.setData({
        isEdit: options.isEdit,
        cententDetail: this.data.cententDetail,
        detailAddress: this.data.cententDetail.address,
        backImg,
        frontImg,
      })
    }

  },

  onUnload: function () {
    console.log('代理商')
    wx.removeStorageSync('editList')
  },

  onHide: function () {
    wx.removeStorageSync('editList')
  },

  addressDetail: function (e) {
    this.setData({
      address: e.detail
    })
  },

  imgView: function (e) {
    var src = e.target.dataset.url; //获取data-src
    var imgList = [e.target.dataset.url]; //获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },

  formSubmit: function (e) {
    var formdata = e.detail.value;

    var url = '';
    var title = '提交成功'

    let params = {}

    if (this.data.isEdit) {
      url = '/odr/protected/comm/beanCrud/fin.AccountVO/update'
      this.data.cententDetail.auditStatus = 3
      if (!this.data.isDelImg) {
        params = {
          ...formdata,
          ...this.data.cententDetail,
          note: this.data.note,
          address: this.data.detailAddress,
          auditStatus: 0
        }
      } else {
        if (this.uploadUpImgs.data.datalist.length === 0 || this.uploadDownImgs.data.datalist.length === 0) {
          wx.showToast({
            icon: 'error',
            title: '请上传身份证~',
          })
          return
        }
        params = {
          ...formdata,
          ...this.data.cententDetail,
          note: this.data.note,
          address: this.data.detailAddress,
          idcardFrontPath: this.uploadUpImgs.data.datalist[0].imgPath || '',
          idcardBackPath: this.uploadDownImgs.data.datalist[0].imgPath || '',
          auditStatus: 0
        }
      }
    } else {
      url = '/odr/protected/account/openAccount'
      params = {
        ...formdata,
        ...this.data.cententDetail,
        note: this.data.note,
        address: this.data.detailAddress,
        idcardFrontPath: this.uploadUpImgs.data.datalist[0].imgPath,
        idcardBackPath: this.uploadDownImgs.data.datalist[0].imgPath
      }
    }

    console.log(params);

    if (this.data.paramsId !== '') {
      params.id = this.data.paramsId
    }

    if (params.address === undefined) {
      wx.showToast({
        icon: 'error',
        title: '请填完整信息~',
      })
      return
    }
    if (params.certNo === '') {
      wx.showToast({
        icon: 'error',
        title: '请填完整信息~',
      })
      return
    }
    if (params.idcardBackPath === '') {
      wx.showToast({
        icon: 'error',
        title: '请填完整信息~',
      })
      return
    }
    if (params.idcardFrontPath === '') {
      wx.showToast({
        icon: 'error',
        title: '请填完整信息~',
      })
      return
    }
    if (params.legalPerson === '') {
      wx.showToast({
        icon: 'error',
        title: '请填完整信息~',
      })
      return
    }
    if (params.mobile === '') {
      wx.showToast({
        icon: 'error',
        title: '请填完整信息~',
      })
      return
    }
    if (params.receiptAccount === '') {
      wx.showToast({
        icon: 'error',
        title: '请填完整信息~',
      })
      return
    }
    if (params.receiptBank === '') {
      wx.showToast({
        icon: 'error',
        title: '请填完整信息~',
      })
      return
    }
    if (params.receiptBankBranch === '') {
      wx.showToast({
        icon: 'error',
        title: '请填完整信息~',
      })
      return
    }
    if (params.receiptName === '') {
      wx.showToast({
        icon: 'error',
        title: '请填完整信息~',
      })
      return
    }
    if (params.receiptMobile === '') {
      wx.showToast({
        icon: 'error',
        title: '请填完整信息~',
      })
      return
    }

    //请求
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: params
    }, (res) => {
      console.log(res)
      wx.showToast({
        title,
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/finance_account/finance_index',
        })
      }, 1500)

    })
  },

  deleteImg: function (e) {
    this.data.cententDetail.idcardFrontPath = ''
    this.data.cententDetail.idcardBackPath = ''
    this.setData({
      isDelImg: true
    })
  },


  goback: function () {
    wx.navigateBack({})
  }


})